package 作业.Test2范方浩;

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Map<String, List<Student>> map = new HashMap<>();
        // 生成一年级一班的假数据
        List<Student> class1 = new ArrayList<>();
        class1.add(new Student("20230101", "张三", 7));
        class1.add(new Student("20230102", "李四", 8));
        class1.add(new Student("20230103", "王五", 7));
        map.put("一年级一班", class1);

// 生成一年级二班的假数据
        List<Student> class2 = new ArrayList<>();
        class2.add(new Student("20230201", "赵六", 8));
        class2.add(new Student("20230202", "钱七", 7));
        class2.add(new Student("20230203", "孙八", 8));
        map.put("一年级二班", class2);

// 生成二年级一班的假数据
        List<Student> class3 = new ArrayList<>();
        class3.add(new Student("20220101", "周九", 9));
        class3.add(new Student("20220102", "吴十", 8));
        class3.add(new Student("20220103", "郑十一", 9));
        map.put("二年级一班", class3);

// 生成三年级一班的假数据
        List<Student> class4 = new ArrayList<>();
        class4.add(new Student("20210101", "王小明", 10));
        class4.add(new Student("20210102", "李小红", 11));
        class4.add(new Student("20210103", "张小花", 10));
        map.put("三年级一班", class4);

// 生成四年级一班的假数据
        List<Student> class5 = new ArrayList<>();
        class5.add(new Student("20200101", "刘星", 11));
        class5.add(new Student("20200102", "夏雪", 12));
        class5.add(new Student("20200103", "夏雨", 11));
        map.put("四年级一班", class5);
        //使用匿名内部类，遍历年级信息，每个班级的学生按照年龄升序打印
/*        for (Map.Entry<String, List<Student>> entry : map.entrySet()) {
            List<Student> students = entry.getValue();
            students.sort(new  Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.getAge() - o2.getAge();
                }
            });
            for (Student student : students) {
                System.out.println(entry.getKey() + ": "+ student);
            }
        }
        System.out.println("=============");*/
        //使用stream流，遍历年级信息，每个班级的学生按照年龄升序打印
        map.entrySet().stream().forEach(entry -> {
            System.out.println(entry.getKey() + ": ");
            entry   .getValue()
                    .stream()
                    .sorted(Comparator.comparingInt(Student::getAge))
                    .forEach(student -> System.out.println(student));
        });
    }
}
